package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

//套餐管理页面
@EnableCaching
@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;


    //添加套餐（insertSetmealDish实现添加套餐菜品）
    @CacheEvict(value = "setmealCache",key = "#setmealDTO.getCategoryId()")
    @ApiOperation("添加套餐")
    @PostMapping
    public Result insertSetmeal(@RequestBody SetmealDTO setmealDTO) {
        setmealService.insertSetmealService(setmealDTO);
        return Result.success();
    }


    //查询页面分页查询模糊查询和分类表categoryId
    @ApiOperation("查询套餐页面")
    @GetMapping("/page")
    public Result selectSetmeal(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageResult pageResult = setmealService.selectSetmealService(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    ;


    //    删除套餐一个删除多个删除和套餐菜品
    @CacheEvict(value = "setmealCache",allEntries = true)
    @ApiOperation("删除套餐和套餐菜品")
    @DeleteMapping
    public Result deleteSetmealDish(Integer[] ids) {
        setmealService.deleteSetmealDishService(ids);
        return Result.success();
    }


    //查询该行套餐和套餐菜品
    @ApiOperation("查询该行套餐和套餐菜品")
    @GetMapping("/{setmealId}")
    public Result selectSetmealandSetmealDish(@PathVariable Integer setmealId) {
        SetmealVO setmealVO = setmealService.selectSetmealandSetmealDish(setmealId);
        return Result.success(setmealVO);
    }


    //修改套餐
    @CacheEvict(value = "setmealCache",key = "#setmealDTO.getCategoryId()")
    @ApiOperation("修改套餐")
    @PutMapping
    public Result updateSetmealService(@RequestBody SetmealDTO setmealDTO) {
        setmealService.updateSetmealService(setmealDTO);
        return Result.success();
    }


    //修改套餐售卖状态
    @CacheEvict(value = "setmealCache",allEntries = true)
    @PostMapping("/status/{status}")
   public Result updateSetmealStatus(@PathVariable Integer status, long id){
        log.info("aaa"+String.valueOf(status));
        Integer a= Math.toIntExact(id);
        setmealService.updateSetmealServiceStatus(status,a);
        return Result.success();
   }
}